<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/9
 * Time: 18:30
 */

namespace app\admin\controller;

use app\admin\model\PublicNotice as PublicNoticeModel;
class PublicNotice extends Index
{
    public function index()
    {
        return view();
    }

    public function  indexData()
    {
        $publicNoticeModel = new PublicNoticeModel();
        $data = $publicNoticeModel->indexData();
        return json($data);
    }

    //公告添加
    public function add()
    {
        $newData['content'] = input('content');
        $newData['sort'] = input('sort');
        $newData['create_time'] = time();
        $res = \app\admin\model\PublicNotice::create($newData);
        if($res) $this->success('添加成功');
        else $this->error('添加失败');
    }

    //修改状态
    public function changeStatus()
    {
        $id = input('id');
        $status = \app\admin\model\PublicNotice::where('id','eq',$id)->value('status');
        $status == 1 ?$status=2 : $status = 1;
        $res = \app\admin\model\PublicNotice::where('id','eq',$id)->update(['status'=>$status]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else  return ['code'=>-100,'msg'=>'修改失败'];
    }

    //公告修改权重
    public function edit()
    {
        $id = input('get.id');
        $sort = input('get.sort');
        $res = \app\admin\model\PublicNotice::where('id','eq',$id)->update(['sort'=>$sort]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else  return ['code'=>-100,'msg'=>'修改失败'];
    }
}